package com.xmkj.bearbiz.pk;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.common.BearBean.ContentDetailsBean;
import com.common.BearMethods.ActiveDetailsMethods;
import com.common.BearMethods.ZanMethods;
import com.common.adapter.helper.IRecyclerViewHelper;
import com.common.cons.Constans;
import com.common.mvp.BaseMvpActivity;
import com.common.mvp.BasePresenter;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.rxbus.RxBus;
import com.common.rxbus.postevent.RxKeyEvent;
import com.common.utils.EmptyUtils;
import com.common.utils.SPUtils;
import com.common.utils.SizeUtils;
import com.common.widget.DividerDecoration.MarginDecoration;
import com.common.widget.imageview.image.ImageLoaderUtils;
import com.common.widget.recyclerview.refresh.recycleview.XRecyclerView;
import com.xmkj.bearbiz.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.functions.Action1;

/**
 * Create by leo
 * 创建日期：2018/6/26
 * 描述：
 */

public class ContentDetailsActivity extends BaseMvpActivity {
    public static final String ACTIVITY_ID = "activity_id";
    public static final String RECOMMENDDETAILS = "RECOMMENDDETAILS";
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.iv_img)
    ImageView ivImg;
    @BindView(R.id.tv_zan)
    TextView tvZan;
    @BindView(R.id.iv_zan)
    ImageView ivZan;
    @BindView(R.id.tv_see)
    TextView tvSee;
    @BindView(R.id.iv_see)
    ImageView ivSee;
    @BindView(R.id.tv_go_comment)
    TextView tvGoComment;
    @BindView(R.id.recyclerview)
    XRecyclerView recyclerview;
    private CommentAdapter adapter;
    List<ContentDetailsBean.CommentListBean> bean = new ArrayList<>();
    private String activity_id;
    private String reccomend;
    private int dianzanNum;
    private boolean isZan;
    private boolean isFirst;

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_content_details;
    }

    @Override
    protected void onViewCreated() {
        activity_id = getIntent().getStringExtra(ACTIVITY_ID);

        setRecyclerView();
        gotoHttp();


    }


    @Override
    protected void doLogicFunc() {

        attachClickListener(ivZan);
        attachClickListener(tvZan);
        attachClickListener(tvGoComment);
        attachClickListener(ivImg);
        rxManager.add(RxBus.getDefault().toObservable(RxKeyEvent.class).subscribe(new Action1<RxKeyEvent>() {
            @Override
            public void call(RxKeyEvent rxKeyEvent) {
                if (EmptyUtils.isNotEventEmpty(rxKeyEvent, RxKeyEvent.JUADEACTIVE_SUCCESS) && (Boolean) rxKeyEvent.getValue()) {
                    mPageIndex = 1;
                    gotoHttp();


                }
            }
        }));

    }

    @Override
    protected void onViewClicked(View view) {
        if (view.getId() == tvGoComment.getId()) {
            Intent intent = new Intent(context, JudgeActivity.class);
            intent.putExtra(JudgeActivity.ACTIVITYID, activity_id);
            startActivity(intent);
        } else if (view.getId() == ivZan.getId() || view.getId() == tvZan.getId()) {
            gotoZan();

        }
    }

    private void gotoZan() {
        showProgressingDialog();
        CommonSubscriber<Object> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                if(isFirst){
                    if(isZan){ // 已经赞过了
                        ivZan.setBackground(getResources().getDrawable(R.mipmap.zan));
                        tvZan.setText((dianzanNum-1)+"");
                        isZan = false;
                    }else {
                        ivZan.setBackground(getResources().getDrawable(R.mipmap.zanle));
                        tvZan.setText((dianzanNum)+"");
                        isZan = true;
                    }
                }else {
                    if(isZan){ // 已经赞过了
                        ivZan.setBackground(getResources().getDrawable(R.mipmap.zan));
                        tvZan.setText((dianzanNum)+"");
                        isZan = false;
                    }else {
                        ivZan.setBackground(getResources().getDrawable(R.mipmap.zanle));
                        tvZan.setText((dianzanNum+1)+"");
                        isZan = true;
                    }
                }


            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });

        int type = 3;
        ZanMethods.getInstance().setLikes(subscriber, SPUtils.getShareInt(Constans.UID), SPUtils.getShareString(Constans.HASHID), type, activity_id);
        rxManager.add(subscriber);
    }

    private void setRecyclerView() {

        IRecyclerViewHelper.init().setRecycleLineLayout(context, LinearLayoutManager.VERTICAL, recyclerview);
        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));
        recyclerview.setHasFixedSize(true);
        recyclerview.setRefreshing(false);
        adapter = new CommentAdapter(context, bean);
        recyclerview.setAdapter(adapter);
   /*     adapter.setOnItemChildClickListener(new CommonAdapter.OnRecyclerViewItemChildClickListener() {
            @Override
            public void onItemChildClick(CommonAdapter adapter, View view, int position) {
                infobean = (AddressEntity.ListsBean) adapter.mData.get(position);
                if (view.getId() == R.id.iv_setdefault) { //删除
                    gotoSetDefault(infobean.addressid);
                } else if (view.getId() == R.id.iv_compile||view.getId()==R.id.tv_compile) { //编辑
                    Intent intent = new Intent(context,AddressModifyActivity.class);
                    intent.putExtra(AddressModifyActivity.FROM,infobean);
                    startActivity(intent);
                } else if (view.getId() == R.id.tv_delete||view.getId()==R.id.iv_delete) { //删除
                    deleteAddress(infobean.addressid,position);
                }
            }
        });*/

        recyclerview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mPageIndex = 1;
                mIsRefreshOrLoadMore = 0;
                gotoHttp();
            }

            @Override
            public void onLoadMore() {
                if (mIsHasNoData) {
                    recyclerview.loadMoreComplete();
                    recyclerview.setNoMore(true);
                    return;
                }
                mPageIndex++;
                mIsRefreshOrLoadMore = 1;
                gotoHttp();
            }
        });
    }

    private void gotoHttp() {
        CommonSubscriber<ContentDetailsBean> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                ContentDetailsBean beans = (ContentDetailsBean) o;
                tvTitle.setText(beans.info.title);
                tvContent.setText(beans.info.content);
                tvSee.setText(beans.info.pageviews + "");
                tvZan.setText(beans.info.likes + "");
                dianzanNum = beans.info.likes;
                isZan = beans.info.is_like != 0;
                isFirst = beans.info.is_like != 0;
                if (beans.info.is_like == 0) {
                    ivZan.setBackground(getResources().getDrawable(R.mipmap.zan));
                } else {
                    ivZan.setBackground(getResources().getDrawable(R.mipmap.zanle));
                }
                ImageLoaderUtils.displayAllImg(ivImg, beans.info.pic_url);
                List<ContentDetailsBean.CommentListBean> beanList = beans.comment_list;
                recyclerview.loadMoreComplete();
                // 下拉刷新
                if (mIsRefreshOrLoadMore == 0) {
                    recyclerview.refreshComplete();
                    adapter.clear();
                }

                if (EmptyUtils.isNotEmpty(beanList)) {
                    bean = beanList;
                    adapter.addAll(bean);
                } else if (mIsRefreshOrLoadMore == 0) {
//                    statusEmpty();
                    return;
                }

                if (EmptyUtils.isEmpty(beanList)) {
                    recyclerview.setNoMore(true);
                } else {
                    mIsHasNoData = beanList.size() < mPageSize;
                    recyclerview.setNoMore(mIsHasNoData);
                }
                adapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                adapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }
        });


        ActiveDetailsMethods.getInstance().activityInfo(subscriber, SPUtils.getShareInt(Constans.UID), SPUtils.getShareString(Constans.HASHID), activity_id, mPageIndex);
        rxManager.add(subscriber);
    }

    @Override
    protected void setNavigation() {
        setNavigationBack("内容详情");
    }


    @Override
    public void bindKnife() {
        ButterKnife.bind(this);
    }
}
